package orest.back.message;

import java.io.IOException;
import java.rmi.ServerException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orest.back.message.MessageHandler;

import net.ssta.core.base.ServletBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;
import net.ssta.core.util.DateUtility;

import net.ssta.common.Constants;
import net.ssta.common.LoginUser;


/**
 @author zhanggk
 
 用户注册、用户中心等相关信息维护
 **/
public class MessageServlet  extends ServletBase{

    protected void defaultAction(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
    }
	
	
    /**
     * 获取用户留言列表
     * 
     * @param request
     * @param response
     */
    public void messageList(HttpServletRequest request,HttpServletResponse response)
    {
    	
			try {
				
				RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
		        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
		        
		        MessageHandler messagehandler = new MessageHandler();
		        RecordSet messageSet = messagehandler.getMessageList(requestKit, pagingInfo);
		        
		        request.setAttribute("messageSet", messageSet);
		        request.setAttribute("pagingInfo", pagingInfo);
		        
		    	forward("/back/message/messageList.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException ex){
				ex.printStackTrace();
			}
    }
    
    
    /**
     * 根据uuid获取指定用户留言的详细信息
     * 
     * @param request
     * @param response
     */
    public void messageShow(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MessageHandler messagehandler = new MessageHandler();
        String uuid = requestKit.getParameter("uuid", "");
        
        Record messageInfo = messagehandler.getMessageById(uuid);
        request.setAttribute("messageInfo", messageInfo);
        Record messagereplyInfo = messagehandler.getMessagereplyById(uuid);
        request.setAttribute("messagereplyInfo", messagereplyInfo);
        
        try{
        	forward("/back/message/messageInfo.jsp");
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException ex){
			ex.printStackTrace();
		}
    }
    
    
    
    /**
     * 删除自荐项目需求信息
     * 
     * @param request
     * @param response
     */
    public void messageDelete(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        MessageHandler messagehandler = new MessageHandler();
        
        String uuids = requestKit.getParameter("uuids", "");
        String ifrecommend = requestKit.getParameter("recommend", "0");
        
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一对审批的信息进行审批状态的改变
        		messagehandler.messageDelete(uuid[i]);
        	}
        }
    }
    
    
    /**
     * 回复用户留言
     * 
     * @param request
     * @param response
     */
    public void replyMessage(HttpServletRequest request,HttpServletResponse response)
    {
    	Connection conn = null;
    	RequestKit requestKit=new RequestKit(request);
    	String forwardurl = "";
    	
    	try {
    		//获取数据库链接
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    	
	    	//从页面上获取技术需求相关内容
	    	Record record = new Record();
	    	String reply = requestKit.getParameter("replyContent", "");
	    	String messageid = requestKit.getParameter("uuid", "");
	    	
	    	//删除以前的回复
	    	MessageHandler messagehandler = new MessageHandler();
	    	messagehandler.deleteReplyByMessageid(messageid);
	    	
	    	//添加用户留言回复
	    	record.setEntityName("T_MESSAGEREPLY");
	    	record.setConnection(conn);
	    	record.setPrimaryKey("ID");
	    	
	    	record.addData("CONTENT", reply);
	    	record.addData("SUBMITDATE", DateUtility.getCurrentDateTime());//提交日期为当前日期
	    	record.addData("USERID", LoginUser.getUserId()); 		//获取登录人员uuid
	    	record.addData("MESSAGEID", messageid);		//是否通过审核，默认设置为0，未审核
	    	
	    	//数据库中插入留言回复信息
	    	Record recordresult = record.create();
	    	
	    	
	    	//修改用户留言的状态
	    	Record replyrecord = new Record();
	    	replyrecord.setEntityName("T_USERMESSAGE");
	    	replyrecord.setConnection(conn);
	    	replyrecord.setPrimaryKey("ID");
	    	replyrecord.addData("ID",messageid);
	    	replyrecord.addData("IFREPLY","1");
	    	
	    	replyrecord.update();
	    	
	    	
		} catch(SQLException s){
			s.printStackTrace();
		}finally{
			//释放数据库连接
			if (conn != null)
	        {
				try
	            {
	                conn.close();
	            }catch (SQLException e){
	                e.printStackTrace();
	            }
	        }
		}	
    }
}


